package javaroke.servlets;

import java.io.IOException;
import java.sql.Date;

import javaroke.negocio.GestionReserva;
import javaroke.excepcion.DAOExcepcion;
import javaroke.modelo.Reserva;
import javaroke.modelo.Sala;
import javaroke.modelo.Cliente;
import javaroke.modelo.Local;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RegistrarReservaServlet
 */
@WebServlet("/RegistrarReservaServlet")
public class RegistrarReservaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegistrarReservaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		try{
			
			GestionReserva negCan = new GestionReserva();
			
			Reserva res = new Reserva();
			Sala sal = new Sala();
			Cliente cli = new Cliente();
			Local loc = new Local();
			
			res.setFecha(request.getParameter("txtFechaReserva"));
			res.setDescripcion(request.getParameter("txtDescripcion"));
			res.setElog(1);
			// Sala
			sal.setIdsala(1);
			res.setSala(sal);
			
			//Cliente
			cli.setIdcliente(2);
			res.setCliente(cli);
			
            //Local
			loc.setIdlocal(3);
			res.setLocal(loc);

						
			try {
				negCan.inserta(res);
								
				request.setAttribute("MENSAJE", "Se registro correctamente.");
				
			} catch (DAOExcepcion e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				request.setAttribute("MENSAJE", e1.getMessage());
			}
		
			
		} catch (IllegalArgumentException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
			request.setAttribute("MENSAJE", "ERROR, Falta llenar campos o tipo de variable no soportada: -" + e2.getMessage() + "-");
	}
		
			RequestDispatcher rd = request
					.getRequestDispatcher("/jsp/RegistraReserva.jsp");
			rd.forward(request, response);
		
	}

}
